package book.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import book.service.BookService;
import book.service.impl.BookServiceImpl;
import book.util.Book;

public class bookServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		BookService as=new BookServiceImpl();
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		//PrintWriter out = response.getWriter();
		HttpSession session=request.getSession();
		String opr=request.getParameter("opr");
			if("findAllbook".equals(opr)){ 
				List<Book> bklist=as.findBookAll1();
				session.setAttribute("bklist", bklist);
			response.sendRedirect("jsp/bookan.jsp");
			}else if("del".equals(opr)){
				Integer Book_id=Integer.parseInt(request.getParameter("book_id"));
				int num=as.deletefrom(Book_id);
				if(num>0){
					response.sendRedirect("bookServlet?opr=findAllbook");
				}else{
					System.out.println("失败");
				}
			}else if("add".equals(opr)){ 
				Integer Book_id=Integer.parseInt(request.getParameter("book_id"));
				String Type=request.getParameter("type");
				String Title=request.getParameter("title");
				String Publisher=request.getParameter("publisher");
				Integer Year=Integer.parseInt(request.getParameter("year"));
				String Author=request.getParameter("author");
				Integer Price=Integer.parseInt(request.getParameter("price"));
				Integer number=Integer.parseInt(request.getParameter("number"));
				Integer stocks=Integer.parseInt(request.getParameter("stocks"));
				
				Book a=new Book(Book_id,Type,Title,Publisher,Year,Author,Price,number,stocks);
				int num=as.insertinto(a);
				if(num>0){
					response.sendRedirect("bookServlet?opr=findAllbook");
				}
			}else if("upd".equals(opr)){
				Integer Book_id=Integer.parseInt(request.getParameter("book_id"));
				String Type=request.getParameter("type"); 
				String Title=request.getParameter("title");
				String Publisher=request.getParameter("publisher");
				
				Integer Year=Integer.parseInt(request.getParameter("year"));
			
				String Author=request.getParameter("author");
				Integer Price=Integer.parseInt(request.getParameter("price"));
				Integer number=Integer.parseInt(request.getParameter("number"));
				Integer stocks=Integer.parseInt(request.getParameter("stocks"));
				Book a=new Book(Book_id,Type,Title,Publisher, Year,Author,Price,number,stocks);
				int num=as.updateset(a);
				if(num>0){
					response.sendRedirect("bookServlet?opr=findAllbook");
				}else{
					System.out.println("失败");
				}
			}
	}
}
